package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.QaappDetailBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.GlideCircle;

/**
 * Created by chenchangxing on 15/12/29.
 */
public class QuestionDetailAdapter extends RecyclerView.Adapter {

    List<QaappDetailBean.ReplyBean> mQaappDetailBeen;
    Context mContext;
    OnSubmitListener mOnSubmitListener;
    int submitPosition = 10000;
    boolean isMine, isSubmit, isChecked, isFirst = true;

    public interface OnSubmitListener {
        public void click(String answer);
    }


    public QuestionDetailAdapter(List<QaappDetailBean.ReplyBean> mQaappDetailBeen) {
        this.mQaappDetailBeen = mQaappDetailBeen;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_detail, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        if (isMine) {

//            if (isFirst) {
//                for (QaappDetailBean.ReplyBean bean : mQaappDetailBeen) {
//                    if (bean.getIscheck().equals("1")) {
//                        isChecked = true;
//                    }
//
//                }
//                isFirst = false;
//            }

//            if () {
//                holder1.mBtnSubmit.setVisibility(View.GONE);
            if (mQaappDetailBeen.get(position).getIscheck().equals("1")) {
                holder1.mBtnSubmit.setVisibility(View.GONE);
            } else {
                holder1.mBtnSubmit.setVisibility(View.VISIBLE);
            }

        } else {
            holder1.mBtnSubmit.setVisibility(View.GONE);

        }

        if (mQaappDetailBeen.get(position).getIscheck().equals("1")) {
            holder1.mImgQuesSubmit.setVisibility(View.VISIBLE);
        } else {
            holder1.mImgQuesSubmit.setVisibility(View.GONE);
        }

        if (isSubmit) {
            holder1.mBtnSubmit.setVisibility(View.GONE);
            if (submitPosition == position) {
                holder1.mImgQuesSubmit.setVisibility(View.VISIBLE);
            }
        }

        holder1.mQuizzerContent.setText(mQaappDetailBeen.get(position).getContent());
        Glide.with(mContext)
                .load(mQaappDetailBeen.get(position).getUthumb())
                .placeholder(R.mipmap.login_head)
                .error(R.mipmap.login_head)
                .transform(new GlideCircle(mContext))
                .into(holder1.mQuizzerIcon);
        holder1.mQuizzerLevel.setText(mQaappDetailBeen.get(position).getExpert());
        if (mQaappDetailBeen.get(position).getExpert().equals("0")) {
            holder1.mQuizzerLevel.setVisibility(View.GONE);
        } else {
            holder1.mQuizzerLevel.setVisibility(View.VISIBLE);
        }
        holder1.mQuizzerTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(mQaappDetailBeen.get(position).getTimeline()) * 1000,
                        System.currentTimeMillis()));
        holder1.mQuizzerName.setText(mQaappDetailBeen.get(position).getNickname());

        holder1.mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnSubmitListener.click(mQaappDetailBeen.get(position).getId());
                submitPosition = position;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mQaappDetailBeen.size();
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_question_detail.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.quizzer_icon)
        ImageView mQuizzerIcon;
        @Bind(R.id.quizzer_name)
        TextView mQuizzerName;
        @Bind(R.id.quizzer_level)
        TextView mQuizzerLevel;
        @Bind(R.id.btn_submit)
        Button mBtnSubmit;
        @Bind(R.id.quizzer_content)
        TextView mQuizzerContent;
        @Bind(R.id.quizzer_time)
        TextView mQuizzerTime;
        @Bind(R.id.img_ques_submit)
        ImageView mImgQuesSubmit;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public boolean isMine() {
        return isMine;
    }

    public void setMine(boolean mine) {
        isMine = mine;
    }

    public boolean isSubmit() {
        return isSubmit;
    }

    public void setSubmit(boolean submit) {
        isSubmit = submit;
    }

    public void setOnSubmitListener(OnSubmitListener onSubmitListener) {
        mOnSubmitListener = onSubmitListener;
    }


}
